package com.etc.layuisys.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.etc.layuisys.dao.UsersDao;
import com.etc.layuisys.entity.UserRowMapper;
import com.etc.layuisys.entity.Users;
import com.etc.layuisys.utils.PageData;

@Repository(value = "usersDao")
public class UsersDaoImpl implements UsersDao {

	@Resource
	private JdbcTemplate jdbcTemplate;

	/**
	 * 分页查询
	 */
	@Override
	public PageData<Users> getUsersByPage(int page, int pageSize, String content) {
		// TODO Auto-generated method stub
		String sql = "select * from users where userName like ?";

		// 统计记录数量
		String selSql = "select count(1) from (" + sql + ") t";

		// 调用getFirst 得到记录数
		Integer count = jdbcTemplate.queryForObject(selSql, Integer.class, "%" + content + "%");
		// 得到起始位置
		int start = (page - 1) * pageSize;

		// 拼接实际的分页的sql语句

		selSql = sql + " limit " + start + "," + pageSize;

		// 查询需要返回的核心数据[指定页码的]
		List<Users> data = jdbcTemplate.query(selSql, new Object[] { "%" + content + "%" }, new UserRowMapper());

		PageData<Users> pd = new PageData<>(data, count, pageSize, page);
		return pd;
	}

	/**
	 * 增加
	 */
	@Override
	public boolean addUsers(Users user) {
		// TODO Auto-generated method stub
		String sql = "insert into users(userName,userPwd,userSex,userTel,userAddress,userLevel,userState) values(?,?,?,?,?,?,?)";
		return jdbcTemplate.update(sql, user.getUserName(), user.getUserPwd(), user.getUserSex(), user.getUserTel(),
				user.getUserAddress(), user.getUserLevel(), user.getUserState()) > 0;
	}

	/**
	 * 
	 */
	@Override
	public Users getUser(Users user) {
		// TODO Auto-generated method stub
		// sql要加管理员判断
		// 查询单行记录
		String sql = "select * from users where userName=? and userPwd =?";

		List<Users> list = (List<Users>) jdbcTemplate.queryForObject(sql,
				new Object[] { user.getUserName(), user.getUserPwd() }, new UserRowMapper());

		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public boolean getUsers(String userName) {
		// TODO Auto-generated method stub
		String sql = "select count(userName) from users where userName=?";
		return jdbcTemplate.queryForObject(sql, Integer.class, userName) > 0;
	}

	/**
	 * 删除用户
	 */
	@Override
	public boolean DeleteUser(int userId) {
		// TODO Auto-generated method stub
		String sql = "delete from users where userId=?";
		
		return jdbcTemplate.update(sql, userId) > 0;
	}

	/**
	 * 修改用户操作
	 */
	@Override
	public boolean updateUser(Users user) {
		// TODO Auto-generated method stub
		String sql = "update users set userName=?,userSex=?,userTel=?,userAddress=?,userLevel=?,userState=? where userId=?";
		int n = jdbcTemplate.update(sql, user.getUserName(), user.getUserSex(), user.getUserTel(),
				user.getUserAddress(), user.getUserLevel(), user.getUserState(), user.getUserId());
		return n > 0;
	}

}
